import time
import cv2
cap = cv2.VideoCapture("../video/basketball1.mp4")
start_time = time.time()
x = 1# displays the frame rate every 1 second
counter = 0
while True:

    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    counter += 1#记录经过多少秒
    if (time.time() - start_time) > x:
        cv2.rectangle(gray, (10, 10), (450, 50), (0, 0, 225), thickness=-1)

        cv2.putText(gray, "FPS {0}" .format(str(counter / (time.time() - start_time))), (10, 35), 2, 1, (255, 0, 255), 1)
        #cv2.putText(gray, "FPS %s" % str(counter / (time.time() - start_time)), (10, 130), 6, 5, (255, 0, 255), 5)
        #cv2.putText(gray, "Hello World!", (400, 50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
        cv2.imshow('frame', gray)
        print("FPS: ", counter / (time.time() - start_time))
        #print(type(str(counter / (time.time() - start_time))))
        #print(type(counter / (time.time() - start_time)))
        counter = 0
        start_time = time.time()
cap.release()
cv2.destroyAllWindows()

